/*
 * @(#)EmailRuleMessageFormatter.java 
 * Copyright (c) 2006 DCIVision Ltd All rights reserved.
 * 
 *  This software is the confidential and proprietary information of DCIVision Ltd 
 *  ("Confidential Information"). You * shall not disclose such Confidential
 *   Information and shall use it only in accordance with the terms of the 
 *   license agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.alert.core;

import java.sql.Connection;

import com.dcivision.alert.bean.UpdateAlertSystemLog;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.Utility;
import com.dcivision.rules.action.NotifyEmailArchiveAction;

/**
 * <p>
 * Class Name: EmailRuleMessageFormatter
 * </p>
 * 
 * @author Greatwall shao
 * @company DCIVision Limited
 * @creation date 2006-11-7
 */
public class EmailRuleMessageFormatter extends AbstractMessageFormatter {
  private String from;

  private String to;

  private String emailSubject;

  private String content;

  private String ruleName;

  private String ruleType ;

  public String getRuleType() {
    return ruleType;
  }

  public void setRuleType(String ruleType) {
    this.ruleType = ruleType;
  }

  public EmailRuleMessageFormatter(UpdateAlertSystemLog bean, SessionContainer sessionContainer,
      Connection dbConn) throws ApplicationException {
    super(bean, sessionContainer, dbConn);
  }

  public String getSender() {
    return "System";
  }

  public String getSubject() {
    String str = "";
    String isList = this.isList() ? "yes" : "";
    str = "<a class=\"strongllink\" href=\"#\" "
        + " title = \" "
        + getSubjectAsTitle()
        + " \" "
        + "onClick=\"OpenCenteredPopup('./EditMessageDetail.do?"
        + "messageType="
        + getMessageType()
        + "&systemLogID="
        + updateAlertSystemLog.getID()
        + "&isList="
        + isList
        + "', 'DCIVisionViewWorkflowProgress', '850', '600', 'status=no,menubar=no,scrollbars=yes,resizable=yes,toolbar=no');\">";
    str += getProperLengSubject();
    str += "</a>";

    return str;
  }

  protected void makeMessageType() {
    setMessageType("emailRuleNotification");
  }

  protected void setSystemMakeSubject() throws ApplicationException {

//  from|to,cc|subject|ruleType|ruleName|ruleDesc|ruleKBFileName
    
    String[] emailInfo = makeEmailInfo();

    makeRuleInfo(emailInfo);
    
    if ("private".equalsIgnoreCase(ruleType)) {
      this.messageSubject = getMessageResource("home.message.emailRule_notification.private",ruleName);
    }
    else {
      this.messageSubject = getMessageResource("home.message.emailRule_notification.public",ruleName);
    }
  }

  private void makeRuleInfo(String[] emailInfo) {

    this.setRuleType(replaceEspecialChar(emailInfo[3]));
    this.setRuleName(replaceEspecialChar(emailInfo[4]));
    
  }
  private String replaceEspecialChar(String inputStr) {
    String outStr = "";
    if (!Utility.isEmpty(inputStr)) {
      outStr = inputStr;
      if (inputStr.indexOf(NotifyEmailArchiveAction.REPLACESTR) != -1) {
        outStr = inputStr.replaceAll(NotifyEmailArchiveAction.REPLACESTR, "\\|");
      }
    }
    return outStr;
  }
  private String[] makeEmailInfo() {
    String message = this.updateAlertSystemLog.getMessage();
    String[] emailInfo = message.split("\\|");
    return emailInfo;
  }

  public String getContent() {
    return content;
  }

  public void setContent(String content) {
    this.content = content;
  }

  public String getFrom() {
    return from;
  }

  public void setFrom(String from) {
    this.from = from;
  }

  public String getTo() {
    return to;
  }

  public void setTo(String to) {
    this.to = to;
  }

  public void setEmailSubject(String subject) {
    this.emailSubject = subject;
  }

  public String getEmailSubject() {
    return emailSubject;
  }

  public String getRuleName() {
    return ruleName;
  }

  public void setRuleName(String ruleName) {
    this.ruleName = ruleName;
  }

  public String getUrlLink() {
    String str = "";
    String isList = this.isList() ? "yes" : "";
    str = "EditMessageDetail.do?"
        + "messageType="
        + getMessageType()
        + "&systemLogID="
        + updateAlertSystemLog.getID()
        + "&isList="
        + isList;

    return str;
  }

 
}
